package com.yuexun.dhdoctor.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.bean.SubListBean;


import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by wangt on 2017/9/12.
 */

public class NurseAddView {

    private Context context;

    public NurseAddView(Context context) {
        this.context = context;
    }

    //预览界面
    public void showView(List<SubListBean> oaTtems, LinearLayout modelView) {
        if (oaTtems != null && oaTtems.size() > 0) {
            addShowView(oaTtems, modelView);
        }
    }

    //编辑界面
    public void editView(List<SubListBean> oaTtems, LinearLayout modelView) {
        if (oaTtems != null && oaTtems.size() > 0) {
            addEditView(oaTtems, modelView);
        }
    }

    //一层标题
    private void addEditView(List<SubListBean> oaTtems, LinearLayout modelView) {
        for (SubListBean item : oaTtems) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(context).inflate(R.layout.add_nurse_topic_one, null);//也可以从XML中加载布局
            view.setLayoutParams(lp);//设置布局参数

            TextView title = (TextView) view.findViewById(R.id.subject_title);
            LinearLayout modelView_1 = (LinearLayout) view.findViewById(R.id.model_view);

//            title.setText(item.getObjectName() + "(" + getOnePoint(item.getSubList()) + "分)");
            title.setText(item.getObjectName());
            addSubView(item.getSubList(), modelView_1);

            modelView.addView(view);
        }
    }

    //二层子标题
    private void addSubView(List<SubListBean> subList, LinearLayout modelView_1) {

        for (int i = 0; i < subList.size(); i++) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(context).inflate(R.layout.add_nurse_topic_one_1, null);//也可以从XML中加载布局
            view.setLayoutParams(lp);//设置布局参数
            TextView title = (TextView) view.findViewById(R.id.subject_title);
            TextView quality = (TextView) view.findViewById(R.id.tv_quality);
            TextView checkway = (TextView) view.findViewById(R.id.tv_checkway);
            LinearLayout llAdd = (LinearLayout) view.findViewById(R.id.ll_add);
            FlowRadioGroup viewGroup = (FlowRadioGroup) view.findViewById(R.id.viewGroup);
            ImageView imgArrow = (ImageView) view.findViewById(R.id.img_arrow_up);

            SpannableStringBuilder styleStringBuilder = new SpannableStringBuilder(subList.get(i).getObjectName() + "(" + new DecimalFormat("#.##").format(subList.get(i).getPoint()) + "分)");
            styleStringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.txt_black1)), 0, subList.get(i).getObjectName().length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            title.setText(styleStringBuilder);
            quality.setText(subList.get(i).getQuality());
            checkway.setText(subList.get(i).getCheckWay());
            addSubItem(subList.get(i).getSubList(), llAdd);
            if (subList.get(i).getPointList().size() > 0) {
                addCheckbox(subList.get(i).getPointList(), imgArrow, viewGroup);
            }

            modelView_1.addView(view);
        }
    }


    private void addSubItem(List<SubListBean> subitem, LinearLayout lladd) {
        for (int i = 0; i < subitem.size(); i++) {
            if (i != (subitem.size() - 1)) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                View view = LayoutInflater.from(context).inflate(R.layout.add_nurse_topic_one_2, null);//也可以从XML中加载布局
                view.setLayoutParams(lp);//设置布局参数

                TextView title = (TextView) view.findViewById(R.id.tv_title);
                title.setText(subitem.get(i).getObjectName());

                lladd.addView(view);
            } else {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                View view = LayoutInflater.from(context).inflate(R.layout.add_nurse_topic_one_2_end, null);//也可以从XML中加载布局
                view.setLayoutParams(lp);//设置布局参数

                TextView title = (TextView) view.findViewById(R.id.tv_title);
                title.setText(subitem.get(i).getObjectName());

                lladd.addView(view);
            }
        }
    }

    //添加扣分项
    private void addCheckbox(final List<SubListBean> pointList, final ImageView imgArrow, final FlowRadioGroup viewGroup) {
        int position = 1;

        Drawable drawable = context.getResources().getDrawable(R.drawable.check_schedule_green);
        CheckBox checkBox_1 = new CheckBox(context);
        checkBox_1.setText(pointList.get(0).getDeductPoints() + "\t" + new DecimalFormat("#.##").format(pointList.get(0).getPoint()) + "分");
        checkBox_1.setTextColor(context.getResources().getColor(R.color.gray));
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        checkBox_1.setCompoundDrawables(null, null, drawable, null);
        checkBox_1.setButtonDrawable(null);
        checkBox_1.setCompoundDrawablePadding(10);
        checkBox_1.setPadding(0, 0, 20, 0);

        if (pointList.size() > 1) {
            CheckBox checkBox_2 = new CheckBox(context);
            checkBox_2.setText(pointList.get(1).getDeductPoints() + "\t" + new DecimalFormat("#.##").format(pointList.get(1).getPoint()) + "分");
            checkBox_2.setTextColor(context.getResources().getColor(R.color.gray));
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            checkBox_2.setCompoundDrawables(null, null, drawable, null);
            checkBox_2.setButtonDrawable(null);
            checkBox_2.setCompoundDrawablePadding(10);
            checkBox_2.setPadding(0, 0, 20, 0);

            checkBox_1.measure(0, 0);
            checkBox_2.measure(0, 0);

            int aaaa = checkBox_1.getMeasuredWidth();
            if (checkBox_1.getMeasuredWidth() <= 700) {
                int bbbb = checkBox_1.getMeasuredWidth() + checkBox_2.getMeasuredWidth();
                if ((checkBox_1.getMeasuredWidth() + checkBox_2.getMeasuredWidth()) <= 700) {
                    position = 2;
                } else {
                    position = 1;
                }
            } else {
                position = 1;
            }
        }

        final int finalPosition = position;
        for (int i = 0; i < position; i++) {
            CheckBox checkBox = new CheckBox(context);
            checkBox.setText(pointList.get(i).getDeductPoints() + "\t" + new DecimalFormat("#.##").format(pointList.get(i).getPoint()) + "分");
            checkBox.setTextColor(context.getResources().getColor(R.color.gray));
            Drawable drawable_1 = context.getResources().getDrawable(R.drawable.check_schedule_green);
            drawable_1.setBounds(0, 0, drawable_1.getMinimumWidth(), drawable_1.getMinimumHeight());
            checkBox.setCompoundDrawables(null, null, drawable_1, null);
            checkBox.setButtonDrawable(null);
            checkBox.setCompoundDrawablePadding(10);
            checkBox.setPadding(0, 0, 20, 0);
            if (pointList.get(i).getChecked() == 1) {
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);
            }
            viewGroup.addView(checkBox);

            final int finalI = i;
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        pointList.get(finalI).setChecked(1);
                    } else {
                        pointList.get(finalI).setChecked(0);
                    }
                }
            });
        }

        if (position == pointList.size()) {
            imgArrow.setVisibility(View.GONE);
        } else {
            imgArrow.setVisibility(View.VISIBLE);
        }

        imgArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imgArrow.getScaleY() == -1) {
                    imgArrow.setScaleY(1);

                    viewGroup.removeAllViews();
                    for (final SubListBean item : pointList) {
                        CheckBox checkBox = new CheckBox(context);
                        checkBox.setText(item.getDeductPoints() + "\t" + new DecimalFormat("#.##").format(item.getPoint()) + "分");
                        checkBox.setTextColor(context.getResources().getColor(R.color.gray));
                        Drawable drawable = context.getResources().getDrawable(R.drawable.check_schedule_green);
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                        checkBox.setCompoundDrawables(null, null, drawable, null);
                        checkBox.setButtonDrawable(null);
                        checkBox.setCompoundDrawablePadding(10);
                        checkBox.setPadding(0, 0, 20, 0);
                        if (item.getChecked() == 1) {
                            checkBox.setChecked(true);
                        } else {
                            checkBox.setChecked(false);
                        }
                        viewGroup.addView(checkBox);

                        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    item.setChecked(1);
                                } else {
                                    item.setChecked(0);
                                }
                            }
                        });
                    }

                } else {
                    imgArrow.setScaleY(-1);

                    viewGroup.removeAllViews();
                    for (int i = 0; i < finalPosition; i++) {
                        CheckBox checkBox = new CheckBox(context);
                        checkBox.setText(pointList.get(i).getDeductPoints() + "\t" + new DecimalFormat("#.##").format(pointList.get(i).getPoint()) + "分");
                        checkBox.setTextColor(context.getResources().getColor(R.color.gray));
                        Drawable drawable = context.getResources().getDrawable(R.drawable.check_schedule_green);
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                        checkBox.setCompoundDrawables(null, null, drawable, null);
                        checkBox.setButtonDrawable(null);
                        checkBox.setCompoundDrawablePadding(10);
                        checkBox.setPadding(0, 0, 20, 0);
                        if (pointList.get(i).getChecked() == 1) {
                            checkBox.setChecked(true);
                        } else {
                            checkBox.setChecked(false);
                        }
                        viewGroup.addView(checkBox);

                        final int finalI = i;
                        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    pointList.get(finalI).setChecked(1);
                                } else {
                                    pointList.get(finalI).setChecked(0);
                                }
                            }
                        });
                    }
                }
            }
        });
    }


    /*---------------------------------------------------------------------------*/

    //一层标题
    private void addShowView(List<SubListBean> oaItems, LinearLayout modelView) {
        for (SubListBean item : oaItems) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(context).inflate(R.layout.add_nurse_topic_two, null);//也可以从XML中加载布局
            view.setLayoutParams(lp);//设置布局参数

            LinearLayout llTitle = (LinearLayout) view.findViewById(R.id.ll_title);
            TextView title = (TextView) view.findViewById(R.id.subject_title);
            final TextView num = (TextView) view.findViewById(R.id.subject_num);
            final ImageView imgArrow = (ImageView) view.findViewById(R.id.img_arrow_up);
            final LinearLayout llContent = (LinearLayout) view.findViewById(R.id.ll_content);

            title.setText(item.getObjectName());
            num.setText("(得分" + new DecimalFormat("#.##").format((getOnePoints(item.getSubList()) - getOneDeductPoints(item.getSubList()))) + ")");

            llTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (imgArrow.getScaleY() == -1) {
                        imgArrow.setScaleY(1);
                        llContent.setVisibility(View.VISIBLE);
//                        num.setVisibility(View.GONE);
                    } else {
                        imgArrow.setScaleY(-1);
                        llContent.setVisibility(View.GONE);
//                        num.setVisibility(View.VISIBLE);
                    }
                }
            });

            addContent(item.getSubList(), llContent);

            modelView.addView(view);
        }
    }

    //获得一级得分
    private double getOnePoints(List<SubListBean> subList) {
        double points = 0;
        for (SubListBean item : subList) {
            points += item.getPoint();
        }
        return points;
    }

    //获得一级减分
    private double getOneDeductPoints(List<SubListBean> subList) {
        double deductPoints = 0;
        for (SubListBean item : subList) {
            for (SubListBean pointitem : item.getPointList()) {
                if (pointitem.getChecked() == 1) {
                    deductPoints += pointitem.getPoint();
                }
            }
        }
        return deductPoints;
    }

    //二层
    private void addContent(List<SubListBean> subList, LinearLayout llContent) {
        for (int i = 0; i < subList.size(); i++) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(context).inflate(R.layout.add_nurse_topic_two_1, null);//也可以从XML中加载布局
            view.setLayoutParams(lp);//设置布局参数
            TextView title = (TextView) view.findViewById(R.id.subject_title);
            TextView getPoint = (TextView) view.findViewById(R.id.tv_getpoint);
            TextView quality = (TextView) view.findViewById(R.id.tv_quality);
            TextView checkway = (TextView) view.findViewById(R.id.tv_checkway);
            LinearLayout llAdd = (LinearLayout) view.findViewById(R.id.ll_add);
            FlowRadioGroup viewGroup = (FlowRadioGroup) view.findViewById(R.id.viewGroup);

            SpannableStringBuilder styleStringBuilder = new SpannableStringBuilder(subList.get(i).getObjectName() + "(" + new DecimalFormat("#.##").format(subList.get(i).getPoint()) + "分)");
            styleStringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.txt_black1)), 0, subList.get(i).getObjectName().length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            title.setText(styleStringBuilder);
            quality.setText(subList.get(i).getQuality());
            checkway.setText(subList.get(i).getCheckWay());
            getPoint.setText(new DecimalFormat("#.##").format(subList.get(i).getPoint() - getTwoDeductPoints(subList.get(i).getPointList())));

            addSubItem(subList.get(i).getSubList(), llAdd);
            addCheckItem(subList.get(i).getPointList(), viewGroup);

            llContent.addView(view);
        }
    }

    //获得二级减分
    private double getTwoDeductPoints(List<SubListBean> pointlist) {
        double deductPoints = 0;
        for (SubListBean item : pointlist) {
            if (item.getChecked() == 1) {
                deductPoints += item.getPoint();
            }
        }
        return deductPoints;
    }

    private void addCheckItem(final List<SubListBean> pointList, final FlowRadioGroup viewGroup) {
        //选中的项
        int checksize = 0;
        for (final SubListBean item : pointList) {
            if (item.getChecked() == 1) {
                checksize++;
                TextView textView = new TextView(context);
                textView.setText(item.getDeductPoints() + "\t" + "分值:" + new DecimalFormat("#.##").format(item.getPoint()) + "分");
                textView.setTextColor(context.getResources().getColor(R.color.red));
                textView.setPadding(0, 0, 20, 0);
                viewGroup.addView(textView);
            }
        }
        if (checksize == 0) {
            TextView textView = new TextView(context);
            textView.setText("无");
            textView.setTextColor(context.getResources().getColor(R.color.red));
            textView.setPadding(0, 0, 20, 0);
            viewGroup.addView(textView);
        }
    }
}
